<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Controller
 *
 * @author lichnow
 */
namespace api\frame;
abstract class core {
    protected $tpl;
    protected $config;
    private $tpl_component;
    private $tpl_default;
    private $tpl_compile;
    abstract public function __construct($config);
    final private function getSuffix(){
        if(isset($this->config['template']['suffix'])){
            $suffix = $this->config['template']['suffix'];
        }else{
            $suffix = ".html";
        }
        return $suffix;
    }
    public function setTplObject($path){
        $this->tpl_component = \core\Mt::getComponent("template")->init($this->config['template']);
        $this->setTplPath($path);
        $this->setTplType();
    }
    public function setTplPath($path){
        if (!isset($this->config['template']['default'])) {
            $this->config['template']['default'] = 'default';
        }
        $this->tpl_default = $path['default'];
        $this->tpl_compile = $path['compile'];
    }
     public function setTplType(){
        if (!isset($this->config['template']['engine']) || $this->config['template']['engine'] == 'tmtview') {
            $this->tpl = $this->tpl_component->tmtview;
            $this->tmtview();
        }
        if ($this->config['template']['engine'] == 'smarty') {
            $this->tpl = $this->tpl_component->smarty;
            $this->setSmartyTpl();
            $this->setSmartyTag();
        }
    }
    public function tmtView(){
        
    }
    public function setSmartyTpl() {
        $this->tpl->setTplDir($this->tpl_default);
        $this->tpl->setComdir($this->tpl_compile);
    }
    public function setSmartyTag(){
        require_once FRM_PATH.'/components/frame/template/frame.tags.php';
        $this->tpl->tplBlock("asset", "asset");
    }
    abstract protected function getTplFile($template);
    final public function setValue($tpl_var, $value = null, $nocache = false) {
        $this->tpl->assign($tpl_var, $value, $nocache);
    }
    final public function loadView($template = null, $cache_id = null, $compile_id = null, $parent = null) {
        $suffix = $this->getSuffix();
        $tplfile = $this->getTplFile($template);
        $template = $tplfile.$suffix;
        $this->tpl->display($template, $cache_id, $compile_id, $parent);
        if(substr_count(get_called_class(),"controller") == 1){
            exit();
        }
    }
    final public function loadModel($model){
        $model = "\\model\\".$model;
        $$model = new $model($this->config['orm']);
        return $$model;
    }
}

?>
